import { WetAirStateByAbsHum } from "../wetAirState";

/** 根据新风比混合处理
 *
 * @param outAir 室外湿空气类
 * @param inAir  室内湿空气类
 * @param ratio  新风比
 * @returns 混合空气状态点类
 */
export const AirMixHandling = (
  outAir: WetAirStateByAbsHum,
  inAir: WetAirStateByAbsHum,
  ratio: number
) => {
  let rt: number;
  if (ratio >= 0 && ratio <= 1) {
    rt = ratio;
  } else if (ratio > 1 && ratio <= 100) {
    rt = ratio / 100;
  } else {
    throw new Error("Argument OutOf Range Exception!!");
  }
  const tm = linearMixPoint(outAir.dbTemp, inAir.dbTemp, ratio);
  const hm = rt * outAir.absHum + (1 - rt) * inAir.absHum;
  return new WetAirStateByAbsHum(tm, hm);
};

const linearMixPoint = (outdoor: number, indoor: number, ratio: number) => {
  const to = Math.max(outdoor, indoor);
  const ti = Math.min(outdoor, indoor);
  return ratio * (to - ti) + ti;
};
